package cn.tmpan.project.common;

import cn.tmpan.project.common.enums.ResponseCode;
//import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jpay.ext.kit.PaymentKit;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Public method.
 *
 * @Author tieMinPan
 * @Desc: Description 公共方法
 * @Date 2019 -01-06 14:27
 */
public class PublicMethod {
    /**
     * @since 分页字段排序分隔符
     */
    private static final String ORDER_BY_TAG = ">>";

    /**
     * 初始化分页公共方法
     *
     * @param pageNum  the page num
     * @param pageSize the page size
     * @param orderBy  the order by
     * @return hash map
     */
    public static Map<String, Object> initPageHelp(Integer pageNum, Integer pageSize, String orderBy) {
        /**
         *@since 构建返回结果
         */
        HashMap<String, Object> res = new HashMap<>(16);
        PageHelper.startPage(pageNum, pageSize);
        if (StringUtils.hasText(orderBy)) {
            String[] arrays = orderBy.split(ORDER_BY_TAG);
            PageHelper.orderBy(arrays[0] + " " + arrays[1]);
        }
        return res;
    }

    /**
     * 获取pageInfo分页数据
     *
     * @param list   sql查询出来list
     * @param voList 实际封装后的list
     * @return
     */
    public static PageInfo getPageInfo(List list, List voList) {
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setList(voList);
        return pageInfo;
    }

    /**
     * 获取pageInfo分页数据
     *
     * @param list
     * @return
     */
    public static PageInfo getPageInfo(List list) {
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 返回给微信处理不再重复发生请求
     *
     * @param returnCode the return code
     * @param returnMsg  the return msg
     * @return xml xml
     */
    public static String setXML(String returnCode, String returnMsg) {
        /**
         *@since 构建方法体
         */
        Map<String, String> xml = new HashMap<>(16);
        xml.put("return_code", returnCode);
        xml.put("return_msg", returnMsg);
        return PaymentKit.toXml(xml);
    }

    /**
     * 返回数据格式
     *
     * @param res
     * @return
     */
    public static ServerResponse getServerResponse(int res) {
        int resData = res;
        if (resData > 0) {
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorCodeMessage(ResponseCode.DATABASE_ERROR.getCode(), ResponseCode.DATABASE_ERROR.getDesc());
    }
}
